package cn.sola.push;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author: kangxiaohui
 * @date：2017年12月20日 下午5:40:42
 * @version 1.0 
 * 
 */
public class SubThread extends Thread{

	 private JedisPool jedisPool;
	 
	 private String channel;
	 
	 private final Subscriber subscriber = new Subscriber();
	 
	 
	public SubThread(JedisPool jedisPool, String channel) {
		super();
		this.jedisPool = jedisPool;
		this.channel = channel;
	}


	@Override
	public void run() {
		System.out.println(String.format("subscribe redis, channel %s, thread will be blocked", channel));
		Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(subscriber, channel);
        } catch (Exception e) {
            System.out.println(String.format("subsrcibe channel error, %s", e));
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
	}

	
}
